package com.heatingcharge.security.handle;

import cn.hutool.http.Status;
import com.heatingcharge.common.util.R;
import com.heatingcharge.security.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * Description: 权限不足处理类 返回403
 * User: Devin
 * Date: 2019-07-05
 * Time: 11:37
 */
@Slf4j
@Component("AccessDeniedHandlerImpl")
public class AccessDeniedHandlerImpl implements AccessDeniedHandler, Serializable {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        log.info("请求: " + request.getRequestURI() + " 权限不足，无法访问系统资源.");
        log.error(e.getMessage());
        SecurityUtil.writeJavaScript(R.error(Status.HTTP_FORBIDDEN,"权限不足,禁止访问"),response);
    }
}
